package com.rw.tool.test.exception.extra;

import com.rw.tool.util.expression.element.Calculation;
import com.rw.tool.util.expression.expression.calculator.element.CalculatorElement;

import java.math.BigDecimal;
import java.util.Random;

public class DiceCalculatorElement extends CalculatorElement {
    /**
     * 判断该计算元素是否为目标元素
     *
     * @param calculation 计算符号
     * @return 是否为目标符号
     */
    @Override
    public boolean trigger(Calculation calculation) {
        return calculation == DiceCalculation.DICE;
    }

    /**
     * 使用目标符号进行计算
     *
     * @param leftNumber  左元素
     * @param calculation 计算符号
     * @param rightNumber 右元素
     * @return 结果
     */
    @Override
    public BigDecimal handler(BigDecimal leftNumber, Calculation calculation, BigDecimal rightNumber) {
        int count = leftNumber.intValue();
        int size = rightNumber.intValue();
        Random random = new Random();
        int sum = 0;

        for (int i = 0; i < count; i++) {
            sum += random.nextInt(size) + 1;
        }
        return new BigDecimal(sum);
    }
}
